<?php
/**
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 3 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
*/

/**
 * View factory class for views
 * @package Core
 * @subpackage View
 * @author pilou@pilou.hu
 * @version 0.2.1 
 */
namespace Core\View;

class View
{
	/**
	 * Instances of views
	 * @access protected
	 * @var array of IViews
	 */
	protected static $_instances = array();
	
	/**
	 * Gets the views instance by $viewName
	 * @access public
	 * @access static
	 * @param string $viewName 
	 */
	public static function getInstance($viewName = "default"){
		if (!isset(self::$_instances[$viewName])){
			$reader = \Util\Config\Cache::getConfig(DIR_ROOT . "/config/Views.ini");
			
			if (is_null($reader->$viewName)){
				throw new \Exception("Missing view: " . $viewName . " in /config/Views.ini");
			}
			if (is_null($reader->$viewName->template_engine)){
				throw new \Exception("Missing view template_engine: " . $viewName . " in /config/Views.ini");
			}
			if (is_null($reader->$viewName->header)){
				throw new \Exception("Missing view header: " . $viewName . " in /config/Views.ini");
			}
			
			$view = "\Core\View\\" . $reader->$viewName->template_engine;
			self::$_instances[$viewName] = new $view(DIR_TEMPLATES);
			
			if (method_exists(self::$_instances[$viewName], "setHeader")){
				$header = "\Core\HTTP\Header\\" . $reader->$viewName->header;
				self::$_instances[$viewName]->setHeader(new $header());
			}
		}
		return self::$_instances[$viewName];
	}
}

?>